package com.mobile.struts.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mobile.business.grid.IBaseBusiness;
import com.mobile.struts.action.tools.ClassDelegate;
import com.mobile.tools.JsonHelp;

public class FormBaseAction extends BaseDispatchAction {
	
	private IBaseBusiness getBusinessBean(){
		return (IBaseBusiness)getWac().getBean(IBaseBusiness.BEAN_ID);
	}
	/**
	 * 显示要编辑的数据
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward ajaxEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		JsonHelp jsonHelp = new JsonHelp();
		
		String uniqueCode=request.getParameter("uniqueCode");
				
		
		String excFields = request.getParameter("excFields");
		
		String clazz = request.getParameter("clazz");
		clazz=ClassDelegate.clazzMap.get(clazz);
		
			
		PrintWriter out=null;
		try {
			
			if(clazz==null||"".equals(clazz)){
				out.println(jsonHelp.getFormFailJson("操作失败，请联系管理员！"));
				return null;
			}
			
			Object pojo=Class.forName(clazz).newInstance();
			
			pojo=(Object)getBusinessBean().findById(pojo.getClass(),uniqueCode);
			out=response.getWriter();
			
			String content = jsonHelp.getFormSuccessJsonExceptFields(pojo, excFields);
			out.println(content);
		} catch(NullPointerException e){
			//e.printStackTrace();
			out.println(jsonHelp.getFormFailJson("找不到您点击的菜单信息！"));
		} catch(Exception e){
			e.printStackTrace();
		} finally{		
			out.close();
		}
		return null;
	}
	/**
	 * 发送数据到后台并保存
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return		
	 */
	public ActionForward ajaxSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String deptId="deptId";
		String userId="userId";
		
		String uniqueCode=request.getParameter("uniqueCode");
		
		String clazz = request.getParameter("clazz");
		clazz=ClassDelegate.clazzMap.get(clazz);
		if(clazz==null||"".equals(clazz)){
			return null;
		}
		
		try {
			Object pojo=Class.forName(clazz).newInstance();
			BeanUtils.populate(pojo, request.getParameterMap());
			
			if(uniqueCode==null||"".equals(uniqueCode)){
				getBusinessBean().save(pojo);
			}else{
				getBusinessBean().mergeNotNull(uniqueCode,pojo);
			}
			
			//getBusinessBean().merge(pojo);
		} catch(Exception e){
			PrintWriter out=null;
			try {				
				out=response.getWriter();
				out.println(JsonHelp.getFormFailJson("保存数据失败！"));
				e.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} finally{
				out.close();
			}
		}
		return null;	
	}
	/**
	 * 删除数据
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return		
	 */
	public ActionForward ajaxDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String deptId="deptId";
		String userId="userId";
		
		String uniqueCode=request.getParameter("uniqueCode");
		
		String clazz = request.getParameter("clazz");
		clazz=ClassDelegate.clazzMap.get(clazz);
		if(clazz==null||"".equals(clazz)){
			return null;
		}
		
		try {
			Object pojo=Class.forName(clazz).newInstance();
			
			if(uniqueCode!=null&&!"".equals(uniqueCode)){
				getBusinessBean().deleteById(pojo.getClass(), uniqueCode);
			}
		} catch(Exception e){
			PrintWriter out=null;
			try {				
				out=response.getWriter();
				out.println(JsonHelp.getFormFailJson("保存数据失败！"));
				e.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} finally{
				out.close();
			}
		}
		return null;	
	}
}
